package emulator.GUI.display;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;

public class VicCanvas extends Canvas implements Observer {
	
	private static final int PIXEL_HEIGHT = 2;
	private static final int PIXEL_WIDTH = 3;

	private static final long serialVersionUID = -8942291577898864075L;
	
	Dimension dimension;
	VicScreen contents;
	
	VicCanvas(int width, int height)
	{
		super();
		dimension = new Dimension(width, height);
		setSize(PIXEL_WIDTH*width, PIXEL_HEIGHT*height);
		contents = new VicScreen(PIXEL_WIDTH, this.getWidth(), PIXEL_HEIGHT, this.getHeight());
		contents.addObserver(this);
		VicDisplayRegistry.registerDisplay("VicScreen", contents);
	}

	/* (non-Javadoc)
	 * @see java.awt.Canvas#paint(java.awt.Graphics)
	 */
	@Override
	public void paint(Graphics g) {
		contents.lock();
        g.drawImage(contents, 0, 0, Color.black, null);
        contents.unlock();
	}

	public Dimension getScreenSize() {
		return dimension;
	}

	/* (non-Javadoc)
	 * @see java.awt.Canvas#update(java.awt.Graphics)
	 */
	@Override
	public void update(Graphics g) {
		paint(g);
	}

	/**
	 * @param arg0
	 * @see emulator.GUI.display.VicScreen#addObserver(java.util.Observer)
	 */
	public void addObserver(Observer arg0) {
		contents.addObserver(arg0);
	}

	@Override
	public void update(Observable o, Object arg) {
		repaint();
	}
}
